package com.woniu.learnhelloworld.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

/**
 * created by woniu201
 * created in 2018/4/20
 */
@RestController
@RequestMapping(value = "/reqtest")
public class ReqController {

    /**
     * GET测试
     */
    @RequestMapping(value = "/gettest", method = RequestMethod.GET)
    public String getTest(HttpServletRequest req){
        Map<String, String[]> paramMap =  req.getParameterMap();
        String paramUrl = "";
        int mapSize = paramMap.size();
        int pos = 1;
        for (String key: paramMap.keySet()){
            if (pos == 1){
                paramUrl += "?" + key + "=" + req.getParameter(key) + "&";
            }else if(pos == mapSize){
                paramUrl += key + "=" + req.getParameter(key);
            }else{
                paramUrl += key + "=" + req.getParameter(key) + "&";
            }
            pos++;
        }

        return req.getRequestURI() + paramUrl;
    }

    /**
    *   POST测试
    */
    @RequestMapping(value = "/posttest", method = RequestMethod.POST)
    public String postTest(HttpServletRequest req) throws  Exception{
        ServletInputStream sis = req.getInputStream();
        StringBuilder content = new StringBuilder();
        byte[] b = new byte[2048];
        int lens = -1;
        while ((lens = sis.read(b)) > 0) {
            content.append(new String(b, 0, lens));
        }
        String strcont = content.toString();// 内容

        //字符串转json对象
        JSONObject jsonObject = JSON.parseObject(strcont);

        //遍历
        Iterator iterator = jsonObject.keySet().iterator();
        while(iterator.hasNext()){
            String key = (String) iterator.next();
            String value = jsonObject.getString(key);

            System.out.println(key + ":" + value);
        }
        return strcont;
    }

    /**
     *  PUT测试
     */
    @RequestMapping(value = "/puttest", method = RequestMethod.PUT)
    public  String putTest(HttpServletRequest req) throws IOException {
        ServletInputStream sis =null;
        try {
            sis =   req.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        StringBuilder content = new StringBuilder();
        byte[] b = new byte[2048];
        int lens = -1;

        while ((lens = sis.read(b)) > 0) {
            content.append(new String(b, 0, lens));
        }
        String strcont = content.toString();// 内容

        //字符串转json对象
        JSONObject jsonObject = JSON.parseObject(strcont);

        //遍历
        Iterator iterator = jsonObject.keySet().iterator();
        while(iterator.hasNext()){
            String key = (String) iterator.next();
            String value = jsonObject.getString(key);

            System.out.println(key + ":" + value);
        }
        return strcont;
    }


    /**
     *  DELETE测试
     */
    @RequestMapping(value = "/deltest", method = RequestMethod.DELETE)
    public String delTest(HttpServletRequest req){
        Map<String, String[]> paramMap =  req.getParameterMap();
        String paramUrl = "";
        int mapSize = paramMap.size();
        int pos = 1;
        for (String key: paramMap.keySet()){
            if (pos == 1){
                paramUrl += "?" + key + "=" + req.getParameter(key) + "&";
            }else if(pos == mapSize){
                paramUrl += key + "=" + req.getParameter(key);
            }else{
                paramUrl += key + "=" + req.getParameter(key) + "&";
            }
            pos++;
        }

        return req.getRequestURI() + paramUrl;
    }

    @RequestMapping(value = "/*", method = RequestMethod.GET)
    public String test(){
        return "aaaaaaaaaaaaaaaaaaaaaaaaaaaa";
    }
}
